package com.taptech.doufu.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.taptech.doufu.R;
import com.taptech.doufu.bean.PersonalPointBean;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.listener.HttpResponseListener;
import com.taptech.doufu.services.personalcenter.PersonalInfoService;
import com.taptech.doufu.ui.activity.base.DiaobaoBaseActivity;
import com.taptech.doufu.util.DiaobaoUtil;
import com.taptech.doufu.util.TMAnalysis;
import com.taptech.doufu.util.UIUtil;
import com.taptech.doufu.net.httputils.HttpResponseObject;
import com.taptech.doufu.ui.view.PersonalPointListView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * Company: DiaoBao
 * </p>
 *
 * @author huangzh
 * @date 2014-3-20
 */
public class PersonalPointsActivity extends DiaobaoBaseActivity implements HttpResponseListener {

    private PersonalPointListView personal_points_listview;
    private List<PersonalPointBean> personalPoints;
    private PersonalPointsAdapter adapter;

    private View headView;
    private TextView myPoints;
    private TextView costPoints;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        addShadeBg();
        setContentView(R.layout.personal_center_activity_points);
        initView();
    }

    private void initView()
    {
        PersonalInfoService.getInstance().loadPersonalPoints(this);
        personal_points_listview = (PersonalPointListView) findViewById(R.id.personal_points_listview);
        headView =  getLayoutInflater().inflate(R.layout.personal_points_header, null);
        myPoints =  headView.findViewById(R.id.personal_center_activity_points_my_points);
        costPoints =  headView.findViewById(R.id.personal_center_activity_points_used_points);
        personal_points_listview.addHeaderView(headView);
    }


    /********************************************************************
     * id | score_name | score_value | score_limit | score_description *
     * ----+--------------------+-------------+-------------+-------------------
     * * 1 | 评论 | 2 | 10 | 用户评论 * 2 | 分享 | 5 | 6 | 内容分享 * 4 | 社交账号绑定 | 10 | 1 |
     * 用户绑定社交账号 * 6 | 参与话题 | 5 | 6 | 参与话题 * 7 | 邀请好友 | 20 | 10 | 用户邀请好友 *
     ********************************************************************/
    @Override
    public void handleResponse(int handleType, HttpResponseObject response)
    {
        try
        {
            if (response.getStatus() == 0)
            {
                JSONObject data = (JSONObject) response.getData();
                int total = data.getInt("total");
                int cost = data.getInt("cost");
                myPoints.setText("" + total);
                costPoints.setText("" + cost);
                JSONArray array = data.getJSONArray(Constant.LIST);
                personalPoints = new ArrayList<PersonalPointBean>();
                PersonalPointBean personalPoint = null;
                for (int i = 0; i < array.length(); i++)
                {
                    JSONObject scoreData = array.getJSONObject(i);
                    personalPoint = new PersonalPointBean();
                    personalPoint.setJson(scoreData);
                    personalPoints.add(personalPoint);
                }
                adapter = new PersonalPointsAdapter(personalPoints);
                personal_points_listview.setAdapter(adapter);
            } else
            {
                UIUtil.toastMessage(this, DiaobaoUtil.getErrorTips(response));
            }

        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public void back(View view)
    {
        onBackPressed();
    }

    public void sure(View v)
    {
        String url = "https://api.doufu.la/journal/detail?id=231";
        Intent intent = new Intent();
        intent.setClass(this, BrowseActivity.class);
        intent.putExtra(Constant.URL, url);
        intent.putExtra(Constant.TITLE, "豆子攻略");
        startActivity(intent);
    }

    /**
     * 邀请码兑换豆子
     *
     * @param view
     */
    public void getDouzi(View view)
    {
        Intent intent = new Intent(this, PersonalValidateInviteActivity.class);
        startActivity(intent);
    }

    private class PersonalPointsAdapter extends BaseAdapter
    {
        private List<PersonalPointBean> datas;

        public PersonalPointsAdapter(List<PersonalPointBean> datas)
        {
            this.datas = datas;
        }

        @Override
        public int getCount()
        {
            return datas.size();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {

            Holder holder = null;
            if (convertView == null)
            {
                holder = new Holder();
                convertView = View.inflate(parent.getContext(), R.layout.personal_center_adapter_points, null);
                holder.name = (TextView) convertView.findViewById(R.id.personal_center_activity_points_name);
                holder.points = (TextView) convertView.findViewById(R.id.personal_center_activity_points_my_points);
                holder.line = convertView.findViewById(R.id.line);
                convertView.setTag(holder);
            } else
            {
                holder = (Holder) convertView.getTag();
            }
            if (position == 0)
            {
                holder.line.setVisibility(View.GONE);
            }
            holder.name.setText(datas.get(position).getScore_name());
            holder.points.setText(datas.get(position).getScore());
            return convertView;
        }

        @Override
        public Object getItem(int position)
        {
            return datas.get(position);
        }

        @Override
        public long getItemId(int position)
        {
            return position;
        }

    }

    private static class Holder
    {
        TextView name, points;
        View line;
    }
}
